/**
 * 标准类
 */
import ModelingClass from './ModelingClass.js';
import GenericType from './GenericType.js';

export default class StandardClass extends ModelingClass {

    /**
     * 是否抽象类
     * @type {boolean}
     */
    abstractClass = false;
    superClassId = '';
    /**
     * 指定的泛型清单
     * @type {[GenericType]}
     */
    instantiatedGenericTypes = [];

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let clazz = new StandardClass(object.kind, object.packageName, object.simpleName);
        clazz.update(object);
        return clazz;
    }

    update(object) {
        super.update(object);
        if (typeof object === 'object') {
            if (object.instantiatedGenericTypes) {
                this.instantiatedGenericTypes = object.instantiatedGenericTypes.map(type => {
                    return GenericType.of(type);
                });
            }
        }
        return this;
    }

    clone() {
        return StandardClass.of(this);
    }

    getPrimaryProperty() {
        return this.properties ? this.properties.find(p => p.primary) : null;
    }

    findDuplicatedPropertyName() {
        if (this.properties?.length) {
            let seen = new Map();
            for (let property of this.properties) {
                if (seen.has(property.name)) {
                    return property.name;
                }
                seen.set(property.name, true);
            }
        }
        return null;
    }

}
